package net.ant.watercalc.web.service.impl;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import com.xiaoleilu.hutool.date.DateUtil;
import com.xiaoleilu.hutool.exceptions.ExceptionUtil;
import com.xiaoleilu.hutool.io.FileUtil;
import com.xiaoleilu.hutool.util.ArrayUtil;
import net.ant.watercalc.web.pojo.bo.FormData;
import net.ant.watercalc.web.pojo.bo.MailBo;
import net.ant.watercalc.web.service.MailService;
import net.ant.watercalc.web.service.ReportService;
import net.ant.watercalc.web.utils.PdfUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;

import java.io.*;
import java.math.BigDecimal;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.stream.Collectors;

@Service
public class ReportServiceImpl implements ReportService {

    private Logger logger = LoggerFactory.getLogger(this.getClass().getName());

    @Autowired
    MailService mailService;

    @Override
    public byte[] getReportData(FormData formData) throws IOException {
        byte[] datas = new byte[]{};
        try {
            Document doc = new Document(PageSize.A4, 20, 20, 20, 20);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            PdfWriter pdfWriter = PdfWriter.getInstance(doc, baos);
            doc.open();

            Font fontTitle = new Font();
            fontTitle.setSize(20);
            fontTitle.setStyle(1);
            BaseFont bfChinese = BaseFont.createFont("fonts/msyh.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            Font font8 = FontFactory.getFont(Font.FontFamily.UNDEFINED.name(), 8, Font.NORMAL);
            Font font8_cn = new Font(bfChinese, 8, Font.NORMAL);

            //top logo
            PdfContentByte canvas = pdfWriter.getDirectContent();
            canvas.addImage(PdfUtil.getLogoTop(doc));

            Chunk titleChunk1 = new Chunk("Guardsman ", FontFactory.getFont(Font.FontFamily.UNDEFINED.name(), 20, 1));
            ColumnText.showTextAligned(pdfWriter.getDirectContent(), Element.ALIGN_LEFT, new Phrase(titleChunk1), 110, 800, 0);
            Chunk titleChunk2 = new Chunk("TM", FontFactory.getFont(Font.FontFamily.UNDEFINED.name(), 10, 1));
            ColumnText.showTextAligned(pdfWriter.getDirectContent(), Element.ALIGN_LEFT, new Phrase(titleChunk2), 219, 810, 0);
            Chunk titleChunk3 = new Chunk(" Antiscalant Proposal", FontFactory.getFont(Font.FontFamily.UNDEFINED.name(), 20, 1));
            ColumnText.showTextAligned(pdfWriter.getDirectContent(), Element.ALIGN_LEFT, new Phrase(titleChunk3), 237, 800, 0);

            Chunk dateChunk = new Chunk("Date: " + new SimpleDateFormat("MM/dd/yyyy").format(new Date()), FontFactory.getFont(Font.FontFamily.UNDEFINED.name(), 14));
            dateChunk.setUnderline(1F, -3);
            ColumnText.showTextAligned(pdfWriter.getDirectContent(), Element.ALIGN_LEFT, new Phrase(dateChunk), 480, 800, 0);

            //Customer Detail
            Chunk detail = new Chunk("Customer details", FontFactory.getFont(Font.FontFamily.UNDEFINED.name(), 14, 3));
            detail.setUnderline(1F, -3);
            ColumnText.showTextAligned(pdfWriter.getDirectContent(), Element.ALIGN_LEFT, new Phrase(detail), 13, 750, 0);

            PdfPTable detailTable = new PdfPTable(new float[]{1});
            detailTable.setWidthPercentage(100);
            detailTable.setTotalWidth(570);

            Font cellFont = new Font(bfChinese, 10);
            Font headFont = FontFactory.getFont(Font.FontFamily.UNDEFINED.name(), 10, 1);

            PdfPCell cell1 = new PdfPCell(new Phrase("Customer Name: " + (formData.getCustomer() == null ? "/" : formData.getCustomer()), cellFont));
            cell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
            detailTable.addCell(cell1);
            detailTable.addCell(new Phrase("Project Name: " + (formData.getProject() == null ? "/" : formData.getProject()), cellFont));
            detailTable.addCell(new Phrase("Email Address: " + (formData.getEmail() == null ? "/" : formData.getEmail()), cellFont));
            detailTable.addCell(new Phrase("Analysis Name: " + (formData.getAnalysis() == null ? "/" : formData.getAnalysis()), cellFont));
            detailTable.writeSelectedRows(0, 4, 13, 740, pdfWriter.getDirectContent());

            //System Information
            Chunk info = new Chunk("System Information", FontFactory.getFont(Font.FontFamily.UNDEFINED.name(), 14, 3));
            info.setUnderline(1F, -3);
            ColumnText.showTextAligned(pdfWriter.getDirectContent(), Element.ALIGN_LEFT, new Phrase(info), 13, 660, 0);

            PdfPTable infoTable = new PdfPTable(new float[]{285F, 285F});
            infoTable.setWidthPercentage(100);
            infoTable.setTotalWidth(570);

            infoTable.addCell(new Phrase("Feed Temperature: " + formData.getTemperature() + " " + formData.getTempUnit(), cellFont));
            infoTable.addCell(new Phrase("Water Source: " + (formData.getWaterSrc() == null ? "/" : formData.getWaterSrc()), cellFont));
            infoTable.addCell(new Phrase("Feed Flow: " + formData.getFeedFlow() + " " + (formData.getFlowUnit() == null ? "m³/h" : formData.getFlowUnit()), cellFont));
            infoTable.addCell(new Phrase("Product Flow: " + formData.getProductFlow() + " " + (formData.getFlowUnit() == null ? "m³/h" : formData.getFlowUnit()), cellFont));
            infoTable.addCell(new Phrase("Concentrade Flow: " + formData.getConcentradeFlow() + " " + (formData.getFlowUnit() == null ? "m³/h" : formData.getFlowUnit()), cellFont));
            infoTable.addCell(new Phrase("Recovery(%): " + formData.getSystemRecv(), cellFont));
            infoTable.addCell(new Phrase("Membrane Type: " + (formData.getMembraneType() == null ? "/" : formData.getMembraneType()), cellFont));
            infoTable.addCell(new Phrase("Chloride Rejection(%): " + formData.getChloride(), cellFont));
            infoTable.addCell(new Phrase("Raw water pH: " + formData.getRawPh(), cellFont));
            infoTable.addCell(new Phrase("Feed CO2: " + formData.getFeedCo2() + " mg/L as CO2", cellFont));
            infoTable.addCell(new Phrase("Chem dose: " + formData.getChemDos() + " ppm(100%)", cellFont));
            infoTable.addCell(new Phrase("Dosing Chemical: " + formData.getDosing(), cellFont));
            infoTable.writeSelectedRows(0, 6, 13, 650, pdfWriter.getDirectContent());


            //Water Analyse
            Chunk waterAnalyse = new Chunk("Water Analyses/Composition(mg/L)", FontFactory.getFont(Font.FontFamily.UNDEFINED.name(), 14, 3));
            waterAnalyse.setUnderline(1F, -3);
            ColumnText.showTextAligned(pdfWriter.getDirectContent(), Element.ALIGN_LEFT, new Phrase(waterAnalyse), 13, 540, 0);

            PdfPTable waterTable = new PdfPTable(new float[]{155F, 130F, 155F, 130F});
            waterTable.setWidthPercentage(100);
            waterTable.setTotalWidth(570);
            waterTable.addCell(new Phrase("Ion", headFont));
            PdfPCell head_cell_1 = new PdfPCell(new Phrase("Raw Water", headFont));
            head_cell_1.setHorizontalAlignment(Element.ALIGN_CENTER);
            waterTable.addCell(head_cell_1);
            PdfPCell head_cell_2 = new PdfPCell(new Phrase("Feed Water", headFont));
            head_cell_2.setHorizontalAlignment(Element.ALIGN_CENTER);
            waterTable.addCell(head_cell_2);
            PdfPCell head_cell_3 = new PdfPCell(new Phrase("Concentrade", headFont));
            head_cell_3.setHorizontalAlignment(Element.ALIGN_CENTER);
            waterTable.addCell(head_cell_3);

            waterTable.addCell(new Phrase("Ammonium(NH4)", cellFont));
            PdfPCell head_cell_4 = new PdfPCell(new Phrase((formData.getRawCation() == null ? 0 : getFormattedData(formData.getRawCation().getNh4()[0])) + "", cellFont));
            head_cell_4.setHorizontalAlignment(Element.ALIGN_CENTER);
            waterTable.addCell(head_cell_4);
            PdfPCell head_cell_5 = new PdfPCell(new Phrase((formData.getAdjustCation() == null ? 0 : getFormattedData(formData.getAdjustCation().getNh4()[0])) + "", cellFont));
            head_cell_5.setHorizontalAlignment(Element.ALIGN_CENTER);
            waterTable.addCell(head_cell_5);
            PdfPCell head_cell_6 = new PdfPCell(new Phrase((formData.getConcentradeCation() == null ? 0 : getFormattedData(formData.getConcentradeCation().getNh4()[0])) + "", cellFont));
            head_cell_6.setHorizontalAlignment(Element.ALIGN_CENTER);
            waterTable.addCell(head_cell_6);

            waterTable.addCell(new Phrase("Potassium(K)", cellFont));
            PdfPCell head_cell_7 = new PdfPCell(new Phrase((formData.getRawCation() == null ? 0 : getFormattedData(formData.getRawCation().getK()[0])) + "", cellFont));
            head_cell_7.setHorizontalAlignment(Element.ALIGN_CENTER);
            waterTable.addCell(head_cell_7);
            PdfPCell head_cell_8 = new PdfPCell(new Phrase((formData.getAdjustCation() == null ? 0 : getFormattedData(formData.getAdjustCation().getK()[0])) + "", cellFont));
            head_cell_8.setHorizontalAlignment(Element.ALIGN_CENTER);
            waterTable.addCell(head_cell_8);
            PdfPCell head_cell_9 = new PdfPCell(new Phrase((formData.getConcentradeCation() == null ? 0 : getFormattedData(formData.getConcentradeCation().getK()[0])) + "", cellFont));
            head_cell_9.setHorizontalAlignment(Element.ALIGN_CENTER);
            waterTable.addCell(head_cell_9);

            waterTable.addCell(new Phrase("Sodium(Na)", cellFont));
            PdfPCell head_cell_10 = new PdfPCell(new Phrase((formData.getRawCation() == null ? 0 : getFormattedData(formData.getRawCation().getNa()[0])) + "", cellFont));
            head_cell_10.setHorizontalAlignment(Element.ALIGN_CENTER);
            waterTable.addCell(head_cell_10);
            PdfPCell head_cell_11 = new PdfPCell(new Phrase((formData.getAdjustCation() == null ? 0 : getFormattedData(formData.getAdjustCation().getNa()[0])) + "", cellFont));
            head_cell_11.setHorizontalAlignment(Element.ALIGN_CENTER);
            waterTable.addCell(head_cell_11);
            PdfPCell head_cell_12 = new PdfPCell(new Phrase((formData.getConcentradeCation() == null ? 0 : getFormattedData(formData.getConcentradeCation().getNa()[0])) + "", cellFont));
            head_cell_12.setHorizontalAlignment(Element.ALIGN_CENTER);
            waterTable.addCell(head_cell_12);

            waterTable.addCell(new Phrase("Magnesium(Mg)", cellFont));
            PdfPCell head_cell_13 = new PdfPCell(new Phrase((formData.getRawCation() == null ? 0 : getFormattedData(formData.getRawCation().getMg()[0])) + "", cellFont));
            head_cell_13.setHorizontalAlignment(Element.ALIGN_CENTER);
            waterTable.addCell(head_cell_13);
            PdfPCell head_cell_14 = new PdfPCell(new Phrase((formData.getAdjustCation() == null ? 0 : getFormattedData(formData.getAdjustCation().getMg()[0])) + "", cellFont));
            head_cell_14.setHorizontalAlignment(Element.ALIGN_CENTER);
            waterTable.addCell(head_cell_14);
            PdfPCell head_cell_15 = new PdfPCell(new Phrase((formData.getConcentradeCation() == null ? 0 : getFormattedData(formData.getConcentradeCation().getMg()[0])) + "", cellFont));
            head_cell_15.setHorizontalAlignment(Element.ALIGN_CENTER);
            waterTable.addCell(head_cell_15);

            waterTable.addCell(new Phrase("Calcium(Ca)", cellFont));
            PdfPCell head_cell_16 = new PdfPCell(new Phrase((formData.getRawCation() == null ? 0 : getFormattedData(formData.getRawCation().getCa()[0])) + "", cellFont));
            head_cell_16.setHorizontalAlignment(Element.ALIGN_CENTER);
            waterTable.addCell(head_cell_16);
            PdfPCell head_cell_17 = new PdfPCell(new Phrase((formData.getAdjustCation() == null ? 0 : getFormattedData(formData.getAdjustCation().getCa()[0])) + "", cellFont));
            head_cell_17.setHorizontalAlignment(Element.ALIGN_CENTER);
            waterTable.addCell(head_cell_17);
            PdfPCell head_cell_18 = new PdfPCell(new Phrase((formData.getConcentradeCation() == null ? 0 : getFormattedData(formData.getConcentradeCation().getCa()[0])) + "", cellFont));
            head_cell_18.setHorizontalAlignment(Element.ALIGN_CENTER);
            waterTable.addCell(head_cell_18);

            waterTable.addCell(new Phrase("Strontium(Sr)", cellFont));
            PdfPCell head_cell_19 = new PdfPCell(new Phrase((formData.getRawCation() == null ? 0 : getFormattedData(formData.getRawCation().getSr()[0])) + "", cellFont));
            head_cell_19.setHorizontalAlignment(Element.ALIGN_CENTER);
            waterTable.addCell(head_cell_19);
            PdfPCell head_cell_20 = new PdfPCell(new Phrase((formData.getAdjustCation() == null ? 0 : getFormattedData(formData.getAdjustCation().getSr()[0])) + "", cellFont));
            head_cell_20.setHorizontalAlignment(Element.ALIGN_CENTER);
            waterTable.addCell(head_cell_20);
            PdfPCell head_cell_21 = new PdfPCell(new Phrase((formData.getConcentradeCation() == null ? 0 : getFormattedData(formData.getConcentradeCation().getSr()[0])) + "", cellFont));
            head_cell_21.setHorizontalAlignment(Element.ALIGN_CENTER);
            waterTable.addCell(head_cell_21);

            waterTable.addCell(new Phrase("Barium(Ba)", cellFont));
            PdfPCell head_cell_22 = new PdfPCell(new Phrase((formData.getRawCation() == null ? 0 : getFormattedData(formData.getRawCation().getBa()[0])) + "", cellFont));
            head_cell_22.setHorizontalAlignment(Element.ALIGN_CENTER);
            waterTable.addCell(head_cell_22);
            PdfPCell head_cell_23 = new PdfPCell(new Phrase((formData.getAdjustCation() == null ? 0 : getFormattedData(formData.getAdjustCation().getBa()[0])) + "", cellFont));
            head_cell_23.setHorizontalAlignment(Element.ALIGN_CENTER);
            waterTable.addCell(head_cell_23);
            PdfPCell head_cell_24 = new PdfPCell(new Phrase((formData.getConcentradeCation() == null ? 0 : getFormattedData(formData.getConcentradeCation().getBa()[0])) + "", cellFont));
            head_cell_24.setHorizontalAlignment(Element.ALIGN_CENTER);
            waterTable.addCell(head_cell_24);

            waterTable.addCell(new Phrase("Carbonate(CO3)", cellFont));
            PdfPCell head_cell_25 = new PdfPCell(new Phrase((formData.getRawAnion() == null ? 0 : getFormattedData(formData.getRawAnion().getCo3()[0])) + "", cellFont));
            head_cell_25.setHorizontalAlignment(Element.ALIGN_CENTER);
            waterTable.addCell(head_cell_25);
            PdfPCell head_cell_26 = new PdfPCell(new Phrase((formData.getAdjustAnion() == null ? 0 : getFormattedData(formData.getAdjustAnion().getCo3()[0])) + "", cellFont));
            head_cell_26.setHorizontalAlignment(Element.ALIGN_CENTER);
            waterTable.addCell(head_cell_26);
            PdfPCell head_cell_27 = new PdfPCell(new Phrase((formData.getConcentradeAnion() == null ? 0 : getFormattedData(formData.getConcentradeAnion().getCo3()[0])) + "", cellFont));
            head_cell_27.setHorizontalAlignment(Element.ALIGN_CENTER);
            waterTable.addCell(head_cell_27);

            waterTable.addCell(new Phrase("Bicarbonate(HCO3)", cellFont));
            PdfPCell head_cell_28 = new PdfPCell(new Phrase((formData.getRawAnion() == null ? 0 : getFormattedData(formData.getRawAnion().getHco3()[0])) + "", cellFont));
            head_cell_28.setHorizontalAlignment(Element.ALIGN_CENTER);
            waterTable.addCell(head_cell_28);
            PdfPCell head_cell_29 = new PdfPCell(new Phrase((formData.getAdjustAnion() == null ? 0 : getFormattedData(formData.getAdjustAnion().getHco3()[0])) + "", cellFont));
            head_cell_29.setHorizontalAlignment(Element.ALIGN_CENTER);
            waterTable.addCell(head_cell_29);
            PdfPCell head_cell_30 = new PdfPCell(new Phrase((formData.getConcentradeAnion() == null ? 0 : getFormattedData(formData.getConcentradeAnion().getHco3()[0])) + "", cellFont));
            head_cell_30.setHorizontalAlignment(Element.ALIGN_CENTER);
            waterTable.addCell(head_cell_30);

            waterTable.addCell(new Phrase("Nitrate(NO3)", cellFont));
            PdfPCell head_cell_31 = new PdfPCell(new Phrase((formData.getRawAnion() == null ? 0 : getFormattedData(formData.getRawAnion().getNo3()[0])) + "", cellFont));
            head_cell_31.setHorizontalAlignment(Element.ALIGN_CENTER);
            waterTable.addCell(head_cell_31);
            PdfPCell head_cell_32 = new PdfPCell(new Phrase((formData.getAdjustAnion() == null ? 0 : getFormattedData(formData.getAdjustAnion().getNo3()[0])) + "", cellFont));
            head_cell_32.setHorizontalAlignment(Element.ALIGN_CENTER);
            waterTable.addCell(head_cell_32);
            PdfPCell head_cell_33 = new PdfPCell(new Phrase((formData.getConcentradeAnion() == null ? 0 : getFormattedData(formData.getConcentradeAnion().getNo3()[0])) + "", cellFont));
            head_cell_33.setHorizontalAlignment(Element.ALIGN_CENTER);
            waterTable.addCell(head_cell_33);

            waterTable.addCell(new Phrase("Chloride(Cl)", cellFont));
            PdfPCell head_cell_34 = new PdfPCell(new Phrase((formData.getRawAnion() == null ? 0 : getFormattedData(formData.getRawAnion().getCl()[0])) + "", cellFont));
            head_cell_34.setHorizontalAlignment(Element.ALIGN_CENTER);
            waterTable.addCell(head_cell_34);
            PdfPCell head_cell_35 = new PdfPCell(new Phrase((formData.getAdjustAnion() == null ? 0 : getFormattedData(formData.getAdjustAnion().getCl()[0])) + "", cellFont));
            head_cell_35.setHorizontalAlignment(Element.ALIGN_CENTER);
            waterTable.addCell(head_cell_35);
            PdfPCell head_cell_36 = new PdfPCell(new Phrase((formData.getConcentradeAnion() == null ? 0 : getFormattedData(formData.getConcentradeAnion().getCl()[0])) + "", cellFont));
            head_cell_36.setHorizontalAlignment(Element.ALIGN_CENTER);
            waterTable.addCell(head_cell_36);

            waterTable.addCell(new Phrase("Fluoride(F)", cellFont));
            PdfPCell head_cell_37 = new PdfPCell(new Phrase((formData.getRawAnion() == null ? 0 : getFormattedData(formData.getRawAnion().getF()[0])) + "", cellFont));
            head_cell_37.setHorizontalAlignment(Element.ALIGN_CENTER);
            waterTable.addCell(head_cell_37);
            PdfPCell head_cell_38 = new PdfPCell(new Phrase((formData.getAdjustAnion() == null ? 0 : getFormattedData(formData.getAdjustAnion().getF()[0])) + "", cellFont));
            head_cell_38.setHorizontalAlignment(Element.ALIGN_CENTER);
            waterTable.addCell(head_cell_38);
            PdfPCell head_cell_39 = new PdfPCell(new Phrase((formData.getConcentradeAnion() == null ? 0 : getFormattedData(formData.getConcentradeAnion().getF()[0])) + "", cellFont));
            head_cell_39.setHorizontalAlignment(Element.ALIGN_CENTER);
            waterTable.addCell(head_cell_39);

            waterTable.addCell(new Phrase("Sulfate(SO4)", cellFont));
            PdfPCell head_cell_40 = new PdfPCell(new Phrase((formData.getRawAnion() == null ? 0 : getFormattedData(formData.getRawAnion().getSo4()[0])) + "", cellFont));
            head_cell_40.setHorizontalAlignment(Element.ALIGN_CENTER);
            waterTable.addCell(head_cell_40);
            PdfPCell head_cell_41 = new PdfPCell(new Phrase((formData.getAdjustAnion() == null ? 0 : getFormattedData(formData.getAdjustAnion().getSo4()[0])) + "", cellFont));
            head_cell_41.setHorizontalAlignment(Element.ALIGN_CENTER);
            waterTable.addCell(head_cell_41);
            PdfPCell head_cell_42 = new PdfPCell(new Phrase((formData.getConcentradeAnion() == null ? 0 : getFormattedData(formData.getConcentradeAnion().getSo4()[0])) + "", cellFont));
            head_cell_42.setHorizontalAlignment(Element.ALIGN_CENTER);
            waterTable.addCell(head_cell_42);

            waterTable.addCell(new Phrase("Silica(SiO2)", cellFont));
            PdfPCell head_cell_43 = new PdfPCell(new Phrase((formData.getRawAnion() == null ? 0 : getFormattedData(formData.getRawAnion().getSio2())) + "", cellFont));
            head_cell_43.setHorizontalAlignment(Element.ALIGN_CENTER);
            waterTable.addCell(head_cell_43);
            PdfPCell head_cell_44 = new PdfPCell(new Phrase((formData.getAdjustAnion() == null ? 0 : getFormattedData(formData.getAdjustAnion().getSio2())) + "", cellFont));
            head_cell_44.setHorizontalAlignment(Element.ALIGN_CENTER);
            waterTable.addCell(head_cell_44);
            PdfPCell head_cell_45 = new PdfPCell(new Phrase((formData.getConcentradeAnion() == null ? 0 : getFormattedData(formData.getConcentradeAnion().getSio2())) + "", cellFont));
            head_cell_45.setHorizontalAlignment(Element.ALIGN_CENTER);
            waterTable.addCell(head_cell_45);

            waterTable.addCell(new Phrase("Al3+", cellFont));
            PdfPCell head_cell_46 = new PdfPCell(new Phrase((formData.getRawCation() == null ? 0 : getFormattedData(formData.getRawCation().getAl())) + "", cellFont));
            head_cell_46.setHorizontalAlignment(Element.ALIGN_CENTER);
            waterTable.addCell(head_cell_46);
            PdfPCell head_cell_47 = new PdfPCell(new Phrase( "/", cellFont));
            head_cell_47.setHorizontalAlignment(Element.ALIGN_CENTER);
            waterTable.addCell(head_cell_47);
            PdfPCell head_cell_48 = new PdfPCell(new Phrase("/", cellFont));
            head_cell_48.setHorizontalAlignment(Element.ALIGN_CENTER);
            waterTable.addCell(head_cell_48);

            waterTable.addCell(new Phrase("Fe+", cellFont));
            PdfPCell head_cell_49 = new PdfPCell(new Phrase((formData.getRawCation() == null ? 0 : getFormattedData(formData.getRawCation().getFe())) + "", cellFont));
            head_cell_49.setHorizontalAlignment(Element.ALIGN_CENTER);
            waterTable.addCell(head_cell_49);
            PdfPCell head_cell_50 = new PdfPCell(new Phrase( "/", cellFont));
            head_cell_50.setHorizontalAlignment(Element.ALIGN_CENTER);
            waterTable.addCell(head_cell_50);
            PdfPCell head_cell_51 = new PdfPCell(new Phrase("/", cellFont));
            head_cell_51.setHorizontalAlignment(Element.ALIGN_CENTER);
            waterTable.addCell(head_cell_51);

            waterTable.addCell(new Phrase("Mn+", cellFont));
            PdfPCell head_cell_52 = new PdfPCell(new Phrase((formData.getRawCation() == null ? 0 : getFormattedData(formData.getRawCation().getMn())) + "", cellFont));
            head_cell_52.setHorizontalAlignment(Element.ALIGN_CENTER);
            waterTable.addCell(head_cell_52);
            PdfPCell head_cell_53 = new PdfPCell(new Phrase( "/", cellFont));
            head_cell_53.setHorizontalAlignment(Element.ALIGN_CENTER);
            waterTable.addCell(head_cell_53);
            PdfPCell head_cell_54 = new PdfPCell(new Phrase("/", cellFont));
            head_cell_54.setHorizontalAlignment(Element.ALIGN_CENTER);
            waterTable.addCell(head_cell_54);

            waterTable.writeSelectedRows(0, 18, 13, 530, pdfWriter.getDirectContent());


            //Scaling Indices
            Chunk indices = new Chunk("Scaling Indices", FontFactory.getFont(Font.FontFamily.UNDEFINED.name(), 14, 3));
            indices.setUnderline(1F, -3);
            ColumnText.showTextAligned(pdfWriter.getDirectContent(), Element.ALIGN_LEFT, new Phrase(indices), 13, 250, 0);

            PdfPTable indicesTable = new PdfPTable(new float[]{180F, 130F, 130F, 130F});
            indicesTable.setWidthPercentage(100);
            indicesTable.setTotalWidth(570);

            indicesTable.addCell(new Phrase("", cellFont));
            PdfPCell indi_head_1 = new PdfPCell(new Phrase("Raw Water", headFont));
            indi_head_1.setHorizontalAlignment(Element.ALIGN_CENTER);
            indicesTable.addCell(indi_head_1);
            PdfPCell indi_head_2 = new PdfPCell(new Phrase("Feed Water", headFont));
            indi_head_2.setHorizontalAlignment(Element.ALIGN_CENTER);
            indicesTable.addCell(indi_head_2);
            PdfPCell indi_head_3 = new PdfPCell(new Phrase("Concentrade", headFont));
            indi_head_3.setHorizontalAlignment(Element.ALIGN_CENTER);
            indicesTable.addCell(indi_head_3);

            indicesTable.addCell(new Phrase("pH", cellFont));
            PdfPCell indi_cell_1 = new PdfPCell(new Phrase((formData.getResultVo() == null ? 0 : getFormattedData(formData.getResultVo()[0].getRaw())) + "", cellFont));
            indi_cell_1.setHorizontalAlignment(Element.ALIGN_CENTER);
            indicesTable.addCell(indi_cell_1);
            PdfPCell indi_cell_2 = new PdfPCell(new Phrase((formData.getResultVo() == null ? 0 : getFormattedData(formData.getResultVo()[0].getAdjusted())) + "", cellFont));
            indi_cell_2.setHorizontalAlignment(Element.ALIGN_CENTER);
            indicesTable.addCell(indi_cell_2);
            PdfPCell indi_cell_3 = new PdfPCell(new Phrase((formData.getResultVo() == null ? 0 : getFormattedData(formData.getResultVo()[0].getConcentrade())) + "", cellFont));
            indi_cell_3.setHorizontalAlignment(Element.ALIGN_CENTER);
            indicesTable.addCell(indi_cell_3);

            indicesTable.addCell(new Phrase("Langelier", cellFont));
            PdfPCell indi_cell_4 = new PdfPCell(new Phrase((formData.getResultVo() == null ? 0 : getFormattedData(formData.getResultVo()[1].getRaw())) + "", cellFont));
            indi_cell_4.setHorizontalAlignment(Element.ALIGN_CENTER);
            indicesTable.addCell(indi_cell_4);
            PdfPCell indi_cell_5 = new PdfPCell(new Phrase((formData.getResultVo() == null ? 0 : getFormattedData(formData.getResultVo()[1].getAdjusted())) + "", cellFont));
            indi_cell_5.setHorizontalAlignment(Element.ALIGN_CENTER);
            indicesTable.addCell(indi_cell_5);
            PdfPCell indi_cell_6 = new PdfPCell(new Phrase((formData.getResultVo() == null ? 0 : getFormattedData(formData.getResultVo()[1].getConcentrade())) + "", cellFont));
            indi_cell_6.setHorizontalAlignment(Element.ALIGN_CENTER);
            indicesTable.addCell(indi_cell_6);

            indicesTable.addCell(new Phrase("Stiff & Davis", cellFont));
            PdfPCell indi_cell_7 = new PdfPCell(new Phrase((formData.getResultVo() == null ? 0 : getFormattedData(formData.getResultVo()[2].getRaw())) + "", cellFont));
            indi_cell_7.setHorizontalAlignment(Element.ALIGN_CENTER);
            indicesTable.addCell(indi_cell_7);
            PdfPCell indi_cell_8 = new PdfPCell(new Phrase((formData.getResultVo() == null ? 0 : getFormattedData(formData.getResultVo()[2].getAdjusted())) + "", cellFont));
            indi_cell_8.setHorizontalAlignment(Element.ALIGN_CENTER);
            indicesTable.addCell(indi_cell_8);
            PdfPCell indi_cell_9 = new PdfPCell(new Phrase((formData.getResultVo() == null ? 0 : getFormattedData(formData.getResultVo()[2].getConcentrade())) + "", cellFont));
            indi_cell_9.setHorizontalAlignment(Element.ALIGN_CENTER);
            indicesTable.addCell(indi_cell_9);

            indicesTable.addCell(new Phrase("Ionic Strength", cellFont));
            PdfPCell indi_cell_10 = new PdfPCell(new Phrase((formData.getResultVo() == null ? 0 : getFormattedData(formData.getResultVo()[3].getRaw())) + "", cellFont));
            indi_cell_10.setHorizontalAlignment(Element.ALIGN_CENTER);
            indicesTable.addCell(indi_cell_10);
            PdfPCell indi_cell_11 = new PdfPCell(new Phrase((formData.getResultVo() == null ? 0 : getFormattedData(formData.getResultVo()[3].getAdjusted())) + "", cellFont));
            indi_cell_11.setHorizontalAlignment(Element.ALIGN_CENTER);
            indicesTable.addCell(indi_cell_11);
            PdfPCell indi_cell_12 = new PdfPCell(new Phrase((formData.getResultVo() == null ? 0 : getFormattedData(formData.getResultVo()[3].getConcentrade())) + "", cellFont));
            indi_cell_12.setHorizontalAlignment(Element.ALIGN_CENTER);
            indicesTable.addCell(indi_cell_12);

            indicesTable.addCell(new Phrase("TDS(mg/L)", cellFont));
            PdfPCell indi_cell_13 = new PdfPCell(new Phrase((formData.getResultVo() == null ? 0 : getFormattedData(formData.getResultVo()[4].getRaw())) + "", cellFont));
            indi_cell_13.setHorizontalAlignment(Element.ALIGN_CENTER);
            indicesTable.addCell(indi_cell_13);
            PdfPCell indi_cell_14 = new PdfPCell(new Phrase((formData.getResultVo() == null ? 0 : getFormattedData(formData.getResultVo()[4].getAdjusted())) + "", cellFont));
            indi_cell_14.setHorizontalAlignment(Element.ALIGN_CENTER);
            indicesTable.addCell(indi_cell_14);
            PdfPCell indi_cell_15 = new PdfPCell(new Phrase((formData.getResultVo() == null ? 0 : getFormattedData(formData.getResultVo()[4].getConcentrade())) + "", cellFont));
            indi_cell_15.setHorizontalAlignment(Element.ALIGN_CENTER);
            indicesTable.addCell(indi_cell_15);

            indicesTable.addCell(new Phrase("CaSO4(%Saturation)", cellFont));
            PdfPCell indi_cell_16 = new PdfPCell(new Phrase((formData.getResultVo() == null ? 0 : getFormattedData(formData.getResultVo()[5].getRaw())) + "", cellFont));
            indi_cell_16.setHorizontalAlignment(Element.ALIGN_CENTER);
            indicesTable.addCell(indi_cell_16);
            PdfPCell indi_cell_17 = new PdfPCell(new Phrase((formData.getResultVo() == null ? 0 : getFormattedData(formData.getResultVo()[5].getAdjusted())) + "", cellFont));
            indi_cell_17.setHorizontalAlignment(Element.ALIGN_CENTER);
            indicesTable.addCell(indi_cell_17);
            PdfPCell indi_cell_18 = new PdfPCell(new Phrase((formData.getResultVo() == null ? 0 : getFormattedData(formData.getResultVo()[5].getConcentrade())) + "", cellFont));
            indi_cell_18.setHorizontalAlignment(Element.ALIGN_CENTER);
            indicesTable.addCell(indi_cell_18);

            indicesTable.addCell(new Phrase("BaSO4(%Saturation)", cellFont));
            PdfPCell indi_cell_19 = new PdfPCell(new Phrase((formData.getResultVo() == null ? 0 : getFormattedData(formData.getResultVo()[6].getRaw())) + "", cellFont));
            indi_cell_19.setHorizontalAlignment(Element.ALIGN_CENTER);
            indicesTable.addCell(indi_cell_19);
            PdfPCell indi_cell_20 = new PdfPCell(new Phrase((formData.getResultVo() == null ? 0 : getFormattedData(formData.getResultVo()[6].getAdjusted())) + "", cellFont));
            indi_cell_20.setHorizontalAlignment(Element.ALIGN_CENTER);
            indicesTable.addCell(indi_cell_20);
            PdfPCell indi_cell_21 = new PdfPCell(new Phrase((formData.getResultVo() == null ? 0 : getFormattedData(formData.getResultVo()[6].getConcentrade())) + "", cellFont));
            indi_cell_21.setHorizontalAlignment(Element.ALIGN_CENTER);
            indicesTable.addCell(indi_cell_21);

            indicesTable.addCell(new Phrase("SrSO4(%Saturation)", cellFont));
            PdfPCell indi_cell_22 = new PdfPCell(new Phrase((formData.getResultVo() == null ? 0 : getFormattedData(formData.getResultVo()[7].getRaw())) + "", cellFont));
            indi_cell_22.setHorizontalAlignment(Element.ALIGN_CENTER);
            indicesTable.addCell(indi_cell_22);
            PdfPCell indi_cell_23 = new PdfPCell(new Phrase((formData.getResultVo() == null ? 0 : getFormattedData(formData.getResultVo()[7].getAdjusted())) + "", cellFont));
            indi_cell_23.setHorizontalAlignment(Element.ALIGN_CENTER);
            indicesTable.addCell(indi_cell_23);
            PdfPCell indi_cell_24 = new PdfPCell(new Phrase((formData.getResultVo() == null ? 0 : getFormattedData(formData.getResultVo()[7].getConcentrade())) + "", cellFont));
            indi_cell_24.setHorizontalAlignment(Element.ALIGN_CENTER);
            indicesTable.addCell(indi_cell_24);

            indicesTable.addCell(new Phrase("CaF2(%Saturation)", cellFont));
            PdfPCell indi_cell_25 = new PdfPCell(new Phrase((formData.getResultVo() == null ? 0 : getFormattedData(formData.getResultVo()[8].getRaw())) + "", cellFont));
            indi_cell_25.setHorizontalAlignment(Element.ALIGN_CENTER);
            indicesTable.addCell(indi_cell_25);
            PdfPCell indi_cell_26 = new PdfPCell(new Phrase((formData.getResultVo() == null ? 0 : getFormattedData(formData.getResultVo()[8].getAdjusted())) + "", cellFont));
            indi_cell_26.setHorizontalAlignment(Element.ALIGN_CENTER);
            indicesTable.addCell(indi_cell_26);
            PdfPCell indi_cell_27 = new PdfPCell(new Phrase((formData.getResultVo() == null ? 0 : getFormattedData(formData.getResultVo()[8].getConcentrade())) + "", cellFont));
            indi_cell_27.setHorizontalAlignment(Element.ALIGN_CENTER);
            indicesTable.addCell(indi_cell_27);

            indicesTable.addCell(new Phrase("SiO2(%Saturation)", cellFont));
            PdfPCell indi_cell_28 = new PdfPCell(new Phrase((formData.getResultVo() == null ? 0 : getFormattedData(formData.getResultVo()[9].getRaw())) + "", cellFont));
            indi_cell_28.setHorizontalAlignment(Element.ALIGN_CENTER);
            indicesTable.addCell(indi_cell_28);
            PdfPCell indi_cell_29 = new PdfPCell(new Phrase((formData.getResultVo() == null ? 0 : getFormattedData(formData.getResultVo()[9].getAdjusted())) + "", cellFont));
            indi_cell_29.setHorizontalAlignment(Element.ALIGN_CENTER);
            indicesTable.addCell(indi_cell_29);
            PdfPCell indi_cell_30 = new PdfPCell(new Phrase((formData.getResultVo() == null ? 0 : getFormattedData(formData.getResultVo()[9].getConcentrade())) + "", cellFont));
            indi_cell_30.setHorizontalAlignment(Element.ALIGN_CENTER);
            indicesTable.addCell(indi_cell_30);

            indicesTable.writeSelectedRows(0, 11, 13, 240, pdfWriter.getDirectContent());

            //****** Page 2 **********
            doc.newPage();
            canvas = pdfWriter.getDirectContent();
            canvas.addImage(PdfUtil.getChart(formData.getImg64(), doc));

            canvas.moveTo(65, 600);
            canvas.lineTo(530, 600);
            canvas.closePathStroke();

            Chunk gapChunk = new Chunk("Maximum Recommended Saturation: 100%", FontFactory.getFont(Font.FontFamily.UNDEFINED.name(), 10));
            ColumnText.showTextAligned(pdfWriter.getDirectContent(), Element.ALIGN_LEFT, new Phrase(gapChunk), 200, 585, 0);

            //Scaling Potential Notification
            Chunk nofiy = new Chunk("Scaling Potential Notification", FontFactory.getFont(Font.FontFamily.UNDEFINED.name(), 14, 3));
            nofiy.setUnderline(1F, -3);
            ColumnText.showTextAligned(pdfWriter.getDirectContent(), Element.ALIGN_LEFT, new Phrase(nofiy), 13, 555, 0);

            PdfPTable notifyTable = new PdfPTable(new float[]{1});
            notifyTable.setWidthPercentage(100);
            notifyTable.setTotalWidth(570);

            String[] notifies = formData.getNotification();

            String[] doNofitication = new String[]{};
            if (notifies != null) {
                if (notifies.length < 9) {
                    String[] tempArr = new String[9 - notifies.length];
                    for (int i = 0; i < tempArr.length; i++) {
                        tempArr[i] = " ";
                    }
                    doNofitication = ArrayUtil.addAll(notifies, tempArr);
                }

                for (int i = 0; i < doNofitication.length; i++) {
                    PdfPCell cellNotify = new PdfPCell();
                    if (i == 0) {
                        cellNotify.setBorder(13);
                    } else if (i < doNofitication.length - 1) {
                        cellNotify.setBorder(12);
                    } else {
                        cellNotify.setBorder(14);
                        cellNotify.setPaddingBottom(5);
                    }
                    cellNotify.addElement(new Phrase(doNofitication[i], font8_cn));
                    notifyTable.addCell(cellNotify);
                }
                notifyTable.writeSelectedRows(0, doNofitication.length, 13, 545, pdfWriter.getDirectContent());
            }

            //Dose Recommendations
            Chunk dose = new Chunk("Dose Recommendations", FontFactory.getFont(Font.FontFamily.UNDEFINED.name(), 14, 3));
            dose.setUnderline(1F, -3);
            ColumnText.showTextAligned(pdfWriter.getDirectContent(), Element.ALIGN_LEFT, new Phrase(dose), 13, 330, 0);

            PdfPTable doseTable = new PdfPTable(new float[]{1});
            doseTable.setWidthPercentage(100);
            doseTable.setTotalWidth(570);

            doseTable.addCell(new Phrase("Product Selected: " + (formData.getProduct() == null ? "" : formData.getProduct()), cellFont));
            doseTable.addCell(new Phrase("Dosage Rate(mg/L): " + formData.getDoseRate(), cellFont));
            doseTable.addCell(new Phrase("Injetion Rate(ml/Min): " + formData.getInjectRate(), cellFont));
            doseTable.addCell(new Phrase("Max Usage(Liters/day): " + formData.getMaxLiters(), cellFont));
            doseTable.addCell(new Phrase("Max Usage(KG/day): " + formData.getMaxKg(), cellFont));
            doseTable.writeSelectedRows(0, 5, 13, 320, pdfWriter.getDirectContent());

            ColumnText.showTextAligned(pdfWriter.getDirectContent(), Element.ALIGN_LEFT, new Phrase("All queries and requests for additional information can be made to:", cellFont), 13, 230, 0);

            canvas.addImage(PdfUtil.getLogo(doc));

            ColumnText.showTextAligned(pdfWriter.getDirectContent(), Element.ALIGN_LEFT, new Phrase("KOBELL Fluid Technology Ltd.", cellFont), 120, 216, 0);
            ColumnText.showTextAligned(pdfWriter.getDirectContent(), Element.ALIGN_LEFT, new Phrase("EMAIL: Service@kobell.net  ", cellFont), 120, 201, 0);
            //ColumnText.showTextAligned(pdfWriter.getDirectContent(), Element.ALIGN_LEFT, new Phrase("             Service@kobell.net           (CN)", cellFont), 13, 194, 0);


            ColumnText.showTextAligned(pdfWriter.getDirectContent(), Element.ALIGN_LEFT, new Phrase("Disclaimer: The information used in the KDP computer program is based on published data, laboratory studies and practical experience.", font8), 13, 175, 0);
            ColumnText.showTextAligned(pdfWriter.getDirectContent(), Element.ALIGN_LEFT, new Phrase("Since the conditions in which the GUARDSMAN antiscalants may be used are outside our control, we cannot warranty the results obtained.", font8), 13, 163, 0);

            ColumnText.showTextAligned(pdfWriter.getDirectContent(), Element.ALIGN_LEFT, new Phrase("免责声明：本报告，是根据", font8_cn), 13, 152, 0);
            ColumnText.showTextAligned(pdfWriter.getDirectContent(), Element.ALIGN_LEFT, new Phrase("KDP", font8), 110, 152, 0);
            ColumnText.showTextAligned(pdfWriter.getDirectContent(), Element.ALIGN_LEFT, new Phrase("程序计算产生，其计算过程中使用了基于公布的数据和标准，以及实验数据、使用经验为来源。", font8_cn), 127, 152, 0);
            ColumnText.showTextAligned(pdfWriter.getDirectContent(), Element.ALIGN_LEFT, new Phrase("阻垢剂可以用于不属于控制范围的环境，但不能保证使用效果，需自行承担由此带来的风险。", font8_cn), 13, 140, 0);



            doc.close();
            datas = baos.toByteArray();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return datas;
    }

    @Override
    public boolean sendReport(FormData formData, byte[] datas) {

        String email = formData.getEmail();
        String company = formData.getCustomer();

        InputStream input = this.getClass().getClassLoader().getResourceAsStream("templates/mail.html");

        String content = new BufferedReader(new InputStreamReader(input)).lines().collect(Collectors.joining(System.lineSeparator()));

        ByteArrayResource dataArray = new ByteArrayResource(datas);
        MailBo mailBo = new MailBo();
        mailBo.setTo(email);
        mailBo.setSubject("Guardsman ™ Antiscalant  Proposal - " + company + " - " + DateUtil.format(new Date(), "dd/MM/yyyy"));
        mailBo.setContent(content);
        mailBo.setInput(dataArray);
        return mailService.sendMail(mailBo);
    }

    private double getFormattedData(double val) {
        return new BigDecimal(val).setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue();
    }
}
